﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
namespace gamexpert
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        List<TextBox> textboxes;
        protected void Page_Load(object sender, EventArgs e)
        {
            renderTable();
        }
        protected void lb_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            if (lb.CommandName == "Remove")
            {
                int id = Int32.Parse(lb.CommandArgument.ToString());
                Cart c = (Cart)Session["cart"];
                c.RemoveProductById(id);
                Session["cart"] = c; 
                Literal1.Text = "<p>Items successfully removed.</p>";
                renderTable();
            }
        }
        protected void lbContinue_Click(object sender, EventArgs e)
        {
            Response.Redirect("Purchase.aspx");   
        }
        public void renderTable()
        {
            Cart c = (Cart)Session["cart"];
            List<Product> ps;
            ps = c.GetProductList();
            ps.Sort();
            if (ps.Count == 0)
            {
                Literal l = new Literal();
                l.Mode = LiteralMode.PassThrough;
                l.Text = "<p>There is no items in shopping cart.</p>";

                //LinkButton lb = new LinkButton();
                //lb.ID = "lbContinue";
                //lb.Text = "Continue shopping.";
                //lb.Click += new EventHandler(lbContinue_Click);
                Panel1.Controls.Add(l);
                //Panel1.Controls.Add(lb);
                lbtnCheckout.Visible = false;
                Label1.Visible = false;
            }
            Table1.Rows.Clear();
            int first = 0;
            textboxes = new List<TextBox>();
            foreach (Product p in ps)
            {
                DataSet ds = DataBroker.getProductById(p.Id);
                p.Title = ds.Tables[0].Rows[0]["title"].ToString();
                p.Price = Double.Parse(ds.Tables[0].Rows[0]["price"].ToString());
                p.ShippingCost = Double.Parse(ds.Tables[0].Rows[0]["shippingcost"].ToString());
                if (first==0)
                {
                    TableHeaderRow trh = new TableHeaderRow();
                    TableHeaderCell tdt1 = new TableHeaderCell();
                    TableHeaderCell tdt2 = new TableHeaderCell();
                    TableHeaderCell tdt21 = new TableHeaderCell();
                    TableHeaderCell tdt3 = new TableHeaderCell();
                    TableHeaderCell tdt4 = new TableHeaderCell();
                    TableHeaderCell tdt5 = new TableHeaderCell();
                    tdt1.Text = "Product Title";
                    tdt2.Text = "Price(S$)";
                    tdt21.Text = "Shipping Cost<br/> per item(S$)";
                    tdt3.Text= "Quantity";
                    tdt4.Text = "Total Amount";
                    tdt5.Text = "Action";
                    trh.Cells.Add(tdt1);
                    trh.Cells.Add(tdt2);
                    trh.Cells.Add(tdt21);
                    trh.Cells.Add(tdt3);
                    trh.Cells.Add(tdt4);
                    trh.Cells.Add(tdt5);
                    Table1.Rows.Add(trh);
                }

                TableRow tr = new TableRow();
                //first cell
                TableCell td = new TableCell();
                //td.Text = p.Id.ToString();
                td.Text = p.Title.ToString();
                tr.Cells.Add(td);

                //Second cell
                TableCell td2 = new TableCell();
                //td.Text = p.Id.ToString();
                td2.Text = String.Format("S${0:f2}", p.Price);
                tr.Cells.Add(td2);

                TableCell td21 = new TableCell();
                //td.Text = p.Id.ToString();
                td21.Text = String.Format("S${0:f2}", p.ShippingCost);
                tr.Cells.Add(td21);

                //third cell
                TableCell td3 = new TableCell();
                //td2.Text = p.Qty.ToString();
                TextBox tb = new TextBox();
                tb.ID = "p_" + p.Id;
                tb.Text = p.Qty.ToString();
                tb.ToolTip = p.Id.ToString();
                tb.Attributes.Add("onblur", "if(!/^[0-9]+$/.test(this.value)){alert('Please fill in valid quantity');this.value='" + tb.Text + "';this.focus();}");
                textboxes.Add(tb);
                td3.Controls.Add(tb);
                tr.Cells.Add(td3);

                //five cell
                TableCell td5 = new TableCell();
                td5.Text = String.Format("S${0:f2}", p.Total());
                tr.Cells.Add(td5);

                //forth cell
                TableCell td4 = new TableCell();
                LinkButton lb = new LinkButton();
                lb.ID = "lbtn_" + p.Id;
                lb.Text = "remove";
                lb.Click += new EventHandler(lb_Click);
                lb.CommandName = "Remove";
                lb.CommandArgument = p.Id.ToString();
                td4.Controls.Add(lb);
                tr.Cells.Add(td4);

                Table1.Rows.Add(tr);
                first++;
            }
            if (first > 0)
            {
               
                //update btn
                TableRow tru = new TableRow();
                TableCell tdu1 = new TableCell();
                TableCell tdu2 = new TableCell();
                TableCell tdu21 = new TableCell();
                TableCell tdu3 = new TableCell();
                TableCell tdu4 = new TableCell();
                TableCell tdu5 = new TableCell();
                tdu1.Text = "";// "Product Title";
                tdu2.Text = "";
                tdu21.Text = "";
                //tdf3.Text = "Total";// "Quantity";
                Button btnUpdate = new Button();
                btnUpdate.ID = "btnUpdate";
                btnUpdate.Text = "Update";
                btnUpdate.CssClass = "btnUpdate";
                btnUpdate.Click += new EventHandler(btnUpdate_Click);
                tdu3.Controls.Add(btnUpdate);
                tdu4.Text = "";// "S$" + c.Total().ToString();// "Total Amount";
                tdu5.Text = "";// "Action";
                tru.Cells.Add(tdu1);
                tru.Cells.Add(tdu2);
                tru.Cells.Add(tdu21);
                tru.Cells.Add(tdu3);
                tru.Cells.Add(tdu4);
                tru.Cells.Add(tdu5);
                Table1.Rows.Add(tru);



                /*
               //footer
               TableRow trf = new TableRow();
               trf.CssClass = "lastrow";
               //TableCell tdf1 = new TableCell();
               //TableCell tdf2 = new TableCell();
               TableCell tdf3 = new TableCell();
               TableCell tdf4 = new TableCell();
               TableCell tdf5 = new TableCell();
               //tdf1.Text = "";// "Product Title";
               //tdf2.Text = "";
               tdf3.ColumnSpan = 4;
               tdf3.HorizontalAlign = HorizontalAlign.Right;
               tdf3.Text = "Total before shipping cost = ";// "Quantity";
               tdf4.Text = "S$"+c.Total().ToString();// "Total Amount";
               tdf5.Text = "";// "Action";
               //trf.Cells.Add(tdf1);
               //trf.Cells.Add(tdf2);
               trf.Cells.Add(tdf3);
               trf.Cells.Add(tdf4);
               trf.Cells.Add(tdf5);
               Table1.Rows.Add(trf);


               //footer2
               TableRow trf2 = new TableRow();
               trf2.CssClass = "lastrow";
               //TableCell tdf21 = new TableCell();
               //TableCell tdf22 = new TableCell();
               TableCell tdf23 = new TableCell();
               TableCell tdf24 = new TableCell();
               TableCell tdf25 = new TableCell();
               //tdf21.Text = "";// "Product Title";
               //tdf22.Text = "";
               tdf23.HorizontalAlign = HorizontalAlign.Right;
               tdf23.ColumnSpan = 4;
               tdf23.Text = "Total shipping cost= ";// "Quantity";
               tdf24.Text = "S$" + c.TotalShippingCost().ToString();// "Total Amount";
               tdf25.Text = "";// "Action";
               //trf2.Cells.Add(tdf21);
               //trf2.Cells.Add(tdf22);
               trf2.Cells.Add(tdf23);
               trf2.Cells.Add(tdf24);
               trf2.Cells.Add(tdf25);
               Table1.Rows.Add(trf2);
               */
                //footer3
                TableRow trf3 = new TableRow();
                trf3.CssClass = "lastrow";
                TableCell tdf31 = new TableCell();
                TableCell tdf32 = new TableCell();
                TableCell tdf321 = new TableCell();
                TableCell tdf33 = new TableCell();
                TableCell tdf34 = new TableCell();
                TableCell tdf35 = new TableCell();
                tdf31.Text = "";// "Product Title";
                tdf32.Text = "";
                tdf321.Text = "";
                tdf33.HorizontalAlign = HorizontalAlign.Right;
                tdf33.Text = "Total= ";// "Quantity";
                tdf34.Text = "S$" + c.finalTotal().ToString();// "Total Amount";
                tdf35.Text = "";// "Action";
                trf3.Cells.Add(tdf31);
                trf3.Cells.Add(tdf32);
                trf3.Cells.Add(tdf321);
                trf3.Cells.Add(tdf33);
                trf3.Cells.Add(tdf34);
                trf3.Cells.Add(tdf35);
                Table1.Rows.Add(trf3);
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Cart c = (Cart)Session["cart"];
            List<Product> ps;
            ps = c.GetProductList();
            foreach (TextBox tb in textboxes)
            {
                Product p=c.FindProductById(Int32.Parse(tb.ToolTip));
                p.Qty = Int32.Parse(tb.Text);
            }
            Literal1.Text = "<p>Successfully updated items quantity</p>";
            renderTable();
        }
        protected void lbtnCheckout_Click(object sender, EventArgs e)
        {
            Session["checkout"] = "yes";
            Response.Redirect("~/member/Payment.aspx");
        }
    }
}
